package com.huxiu;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huxiu.utils.CacheUtils;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

public class WelcomeActivity extends Activity {
    @ViewInject(R.id.welcome1)
    private FrameLayout welcome1;

    @ViewInject(R.id.welcome2)
    private RelativeLayout welcome2;

    @ViewInject(R.id.welcome_icon)
    private ImageView welcome_icon;

    @ViewInject(R.id.welcome_text)
    private TextView welcome_text;
    @ViewInject(R.id.welcome_line)
    private ImageView welcome_line;

    private int TIME = 800;
    public static final String IS_APP_OPENED = "is_app_opened";// 应用是否打开过的Key

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        ViewUtils.inject(this);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                welcome1.setVisibility(View.GONE);
                welcome2.setVisibility(View.VISIBLE);
                init();
            }

        }, 300);
    }

    private void init() {
        //顶部icon的位移和变大动画
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(welcome_icon, "translationY", Utils.dip2px
                (180), Utils.dip2px(10));
        //-100,50,-50,100是可变参数，只是x轴的位移。
        oa1.setDuration(TIME);
        //        oa1.setRepeatCount(1);//重复次数
        oa1.start();
        //        new Handler().postDelayed(new Runnable() {
        //            public void run() {
        //                welcome_line.setVisibility(View.VISIBLE);
        //            }
        //        }, TIME);

        // 缩放，从无到有
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(welcome_line, "scaleX", 0, 0, 0, 0.01f, 0.1f,
                1f);
        //        oa2.setStartDelay(TIME);
        oa2.setDuration(2000);
        //        oa2.setRepeatCount(1);
        //        oa2.setRepeatMode(ValueAnimator.REVERSE);
        oa2.start();

        //底部文字渐显
        ObjectAnimator oa3 = ObjectAnimator.ofFloat(welcome_text, "alpha", 0, 0, 0f, 1f);
        oa3.setDuration(2000);
        oa3.addListener(new MyAnimationListener());
        oa3.start();
    }


    class MyAnimationListener extends AnimatorListenerAdapter {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            //            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
            //            finish();
            boolean isAppOpened = CacheUtils.getBoolean(getApplicationContext(), IS_APP_OPENED, false);
            if (isAppOpened) {// 应用已经打开过，跳到主界面
                System.out.println("跳到主界面");
                startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
            } else {// 应用没有开过，跳到引导界面
                System.out.println("跳到引导界面");
                startActivity(new Intent(WelcomeActivity.this, GuideActivity.class));
            }
            finish();
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}

			